<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>背景颜色更改器</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      html,
      body {
        height: 100%;
      }

      body {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .container {
        display: flex;
        flex-direction: column;
        align-items: center;
      }

      .container h2 {
        background-color: #222;
        color: #fff;
        padding: 20px;
        border-radius: 4px;
        margin-bottom: 20px;
      }

      .container .color {
        color: #49a6e9;
      }

      .container .btn {
        text-transform: uppercase;
        background: transparent;
        color: #222;
        letter-spacing: 2px;
        font-weight: 700;
        transition: all 0.3s linear;
        border: 2px solid #222;
        cursor: pointer;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
        border-radius: 4px;
        font-size: 20px;
        padding: 8px 6px;
      }

      .container .btn:hover {
        color: #fff;
        background: #222;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h2>背景色：<span class="color">green</span></h2>
      <button class="btn">切换</button>
    </div>
    <script>
      const $btn = document.querySelector(".btn");
      const $color = document.querySelector(".color");

      function getRandomColorHex() {
        return "#" + Math.random().toString(16).substr(2, 6);
      }

      function render() {
        const hex = getRandomColorHex();
        document.body.style.backgroundColor = hex;
        $color.textContent = hex;
      }

      $btn.addEventListener("click", function (e) {
        render();
      });
      render();
    </script>
  </body>
</html>
